﻿using LogModule.Interface;
using Prism.Commands;
using Prism.Dialogs;
using Prism.Mvvm;
using StaffModule.DbContexts;
using StaffModule.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace StaffModule.ViewModels {
    public class UpdateStaffViewModel : BindableBase, IDialogAware {
        private readonly ILogService _logService;
        public string Title { get; set; } = " ";
        public int? id { get; set; }
       

        private StaffDetails _StaffDetails;

        public StaffDetails StaffDetails {
            get { return _StaffDetails; }
            set { _StaffDetails = value;
                RaisePropertyChanged();
            }
        }
        private ObservableCollection<int?> _AgeList;

        public ObservableCollection<int?> AgeList {
            get { return _AgeList; }
            set {
                _AgeList = value;
                RaisePropertyChanged();
            }
        }

        private ObservableCollection<string> _PositionList = new ObservableCollection<string>() { "Test1", "Test2", "Test3", "Test4", "Test5" };

        public ObservableCollection<string> PositionList {
            get { return _PositionList; }
            set {
                _PositionList = value;
                RaisePropertyChanged();
            }
        }
        public DelegateCommand UpdateStaffCmd {  get; set; }

        //数据库上下文
        private readonly StaffDbContext _StaffDbContext;
        public UpdateStaffViewModel(StaffDbContext StaffDbContext, ILogService logService) {
            _logService = logService;
            _logService.Info("更新员工弹窗初始化");
            _StaffDbContext = StaffDbContext;
            StaffDetails = new StaffDetails();
            UpdateStaffCmd = new DelegateCommand(UpdateStaff);

            AgeList = new ObservableCollection<int?>();
            for (int i = 18; i <= 60; i++) {
                AgeList.Add(i);
            }


        }
        //更新
        private void UpdateStaff() {
            try {
                _StaffDbContext.StaffDetails.Update(StaffDetails);
                _StaffDbContext.SaveChangesAsync();
                RequestClose.Invoke(ButtonResult.OK);
            } catch (Exception ex){
                Console.WriteLine(ex.Message);
            }
            RequestClose.Invoke(ButtonResult.No);
        }

        public DialogCloseListener RequestClose { get; }
        //是否能够关闭对话框,就是在关闭对话框的时候调用1
        public bool CanCloseDialog() {
            return true;
        }
        //关闭,就是在关闭对话框的时候调用2
        public void OnDialogClosed() {

        }
        //打开对话框,就是在打开对话框的时候调用,传递参数
        public void OnDialogOpened(IDialogParameters parameters) {
            Title = parameters.GetValue<string>("Title");
            id = parameters.GetValue<int?>("id");
            if (id != null && id != 0) {
                StaffDetails = _StaffDbContext.StaffDetails.Where(x => x.Id == id).ToList()[0];
            }
        }

    }
}
